package com.osce.score.controller;

import com.csce.domain.entity.score.ScoreSheet;
import com.csce.domain.entity.score.ScoreSheetStep;
import com.doudou.core.container.ListUtil;
import com.osce.common.annotation.Log;
import com.osce.common.core.controller.BaseController;
import com.osce.common.core.entity.AjaxResult;
import com.osce.common.core.page.TableDataInfo;
import com.osce.common.enums.BusinessType;
import com.osce.common.utils.poi.ExcelExp;
import com.osce.common.utils.poi.ExcelUtil;
import com.osce.common.utils.poi.ExcelUtilManySheet;
import com.osce.interfaces.score.IScoreSheetService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 评分表管理Controller
 *
 * @author zzx
 * @since 2023-08-16
 */
@RestController
@RequestMapping("/score/sheet")
public class ScoreSheetController extends BaseController {

    @Resource
    private IScoreSheetService scoreSheetService;

    /**
     * 查询评分表管理列表
     */
    @PreAuthorize("@ss.hasPermi('score:sheet:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScoreSheet scoreSheet) {
        startPage();
        List<ScoreSheet> list = scoreSheetService.selectScoreSheetList(scoreSheet);
        return getDataTable(list);
    }

    /**
     * 查询评分表管理列表
     */
    @PostMapping("/sheet_str_by_skeetBySkillIds/{skillId}")
    public AjaxResult selectScoreSheetStrByIds(@PathVariable("skillId") Long skillId) {
        return success((Object) scoreSheetService.selectScoreSheetStrByIds(skillId));
    }

    /**
     * 导出评分表管理列表
     */
    @PreAuthorize("@ss.hasPermi('score:sheet:export')")
    @Log(title = "评分表管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScoreSheet scoreSheet) throws IOException {
        List<ExcelExp> list = scoreSheetService.selectScoreSheetExcelList(scoreSheet);
        ExcelUtilManySheet<List<ExcelExp>> util = new ExcelUtilManySheet<>(list);
        util.exportExcelManySheet(response, list);
    }

    /**
     * 导出评分表管理列表-模版
     */
    @PreAuthorize("@ss.hasPermi('score:sheet:export')")
    @Log(title = "评分表管理-模版", businessType = BusinessType.EXPORT)
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        List<ScoreSheetStep> list = ListUtil.list(false);
        ExcelUtil<ScoreSheetStep> util = new ExcelUtil<>(ScoreSheetStep.class);
        util.exportExcel(response, list, "Sheet1", "**题目(没有题目请清空此单元格)");
    }

    /**
     * 导入评分表管理列表
     */
    @PreAuthorize("@ss.hasPermi('score:sheet:import')")
    @Log(title = "评分表管理", businessType = BusinessType.EXPORT)
    @PostMapping("/import/{skillId}")
    public AjaxResult importExcel(@PathVariable("skillId") Long skillId, MultipartFile file) throws Exception {
        return success(scoreSheetService.importExcel(file.getInputStream(), skillId));
    }

    /**
     * 获取评分表管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('score:sheet:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(scoreSheetService.selectScoreSheetById(id));
    }

    /**
     * 新增评分表管理
     */
    @PreAuthorize("@ss.hasPermi('score:sheet:add')")
    @Log(title = "评分表管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScoreSheet scoreSheet) {
        return toAjax(scoreSheetService.insertScoreSheet(scoreSheet));
    }

    /**
     * 修改评分表管理
     */
    @PreAuthorize("@ss.hasPermi('score:sheet:edit')")
    @Log(title = "评分表管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScoreSheet scoreSheet) {
        return toAjax(scoreSheetService.updateScoreSheet(scoreSheet));
    }

    /**
     * 删除评分表管理
     */
    @PreAuthorize("@ss.hasPermi('score:sheet:remove')")
    @Log(title = "评分表管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(scoreSheetService.deleteScoreSheetByIds(ids));
    }
}
